package com.xxl.sso.sample.config;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.filter.XxlSsoWebFilter;
import com.xxl.sso.core.util.JedisUtil;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xuxueli 2018-11-15
 */
@Configuration
public class XxlSsoConfig implements DisposableBean {


    @Value("${xxl.sso.server}")
    private String xxlSsoServer;

    @Value("${xxl.sso.logout.path}")
    private String xxlSsoLogoutPath;

    @Value("${xxl-sso.excluded.paths}")
    private String xxlSsoExcludedPaths;

    @Value("${xxl.sso.redis.address}")
    private String xxlSsoRedisAddress;


    /** 老代码
     * <filter>
     * 	<filter-name>XxlSsoWebFilter</filter-name>
     * 	<filter-class>com.xxl.sso.core.filter.XxlSsoWebFilter</filter-class>
     * 	    <init-param>
     * 	    	<param-name>ERR_URL</param-name>
     * 	    	<param-value>login.html</param-value>
     * 	    </init-param>
     * 	    <init-param> 传参1
     * 	    	<param-name>sso_server</param-name>
     * 	    	<param-value>{xxlSsoServer}</param-value>           http://xxlssoserver.com:8080/xxl-sso-server
     * 	    </init-param>
     * 	    <init-param> 传参2
     * 	    	<param-name>SSO_LOGOUT_PATH</param-name>
     * 	    	<param-value>{xxlSsoLogoutPath}</param-value>       /logout
     * 	    </init-param>
     * 	    <init-param>传参 3
     * 	    	<param-name>SSO_EXCLUDED_PATHS</param-name>
     * 	    	<param-value>{xxlSsoExcludedPaths}</param-value>    null
     * 	    </init-param>
     * </filter>
     * <filter-mapping>
     * 	    <filter-name>XxlSsoWebFilter</filter-name>
     * 	    <url-pattern>/*</url-pattern>
     * </filter-mapping>
     * 原文：https://blog.csdn.net/chenyiminnanjing/article/details/83349820
     * @return
     *
     * < url-pattern>/</url-pattern>  会匹配到/login这样的路径型url，不会匹配到模式为*.jsp这样的后缀型url
     * < url-pattern>/*</url-pattern> 会匹配所有url：路径型的和后缀型的url(包括/login,*.jsp,*.js和*.html等)
     * < url-pattern>/*</url-pattern>   拦截到 --->  XxlSsoWebFilter
     */
    @Bean
    public FilterRegistrationBean xxlSsoFilterRegistration() {
        // xxl-sso, redis init
        JedisUtil.init(xxlSsoRedisAddress);
        // xxl-sso, filter init
        FilterRegistrationBean registration = new FilterRegistrationBean(); // FilterRegistrationBean实现springboot自定义Filter
        registration.setName("XxlSsoWebFilter");// HttpRequestAndResponseFilter <filter-name>XxlSsoWebFilter</filter-name> 关联的<filter-class> 对象： com.xxl.sso.core.filter.XxlSsoWebFilter
        registration.setOrder(1);//多个filter时的执行顺序
        registration.addUrlPatterns("/*");//<url-pattern>/*</url-pattern>
        registration.setFilter(new XxlSsoWebFilter());// <filter-class>com.xxl.sso.core.filter.XxlSsoWebFilter</filter-class>   core模块
        registration.addInitParameter(Conf.SSO_SERVER, xxlSsoServer);                //sso_server           </init-param>   http://xxlssoserver.com:8080/xxl-sso-server
        registration.addInitParameter(Conf.SSO_LOGOUT_PATH, xxlSsoLogoutPath);       //SSO_LOGOUT_PATH      </init-param>   /logout
        registration.addInitParameter(Conf.SSO_EXCLUDED_PATHS, xxlSsoExcludedPaths); //SSO_EXCLUDED_PATHS   </init-param>   null
        // 此filter过滤后访问该client的效果（等同于直接访问）：WebController/login?redirect_url...
        // http://xxlssoserver.com:8080/xxl-sso-server/login?redirect_url=http://localhost:8081/xxl-sso-web-sample-springboot/
        // 实现逻辑： com.xxl.sso.core.filter.XxlSsoWebFilter    xxlssoserver.com需要配置host白名单
        return registration;
    }

    @Override
    public void destroy() throws Exception {

        // xxl-sso, redis close
        JedisUtil.close();
    }

}
